package com.znsb.msfq.album;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.view.fullscreenzoom.PhotoView;
import com.znsb.msfq.view.selectorphoto.Bimp;
import com.znsb.msfq.view.selectorphoto.PublicWay;
import com.znsb.msfq.view.selectorphoto.ViewPagerFixed;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 这个是用于进行图片浏览时的界面
 *
 * @author king
 * @version 2014年10月18日 下午11:47:53
 * @QQ:595163260
 */
public class ViewGalleryActivity extends BaseActivity {
    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.gallery_viewpager)
    ViewPagerFixed galleryViewpager;
    // 获取前一个activity传过来的position
    private int position;
    // 当前的位置
    private int location = 0;

    private ArrayList<View> listViews = null;
    private MyPageAdapter adapter;

    @Override
    protected int getlayoutView() {
        return R.layout.plugin_camera_gallery;
    }

    @Override
    protected void initView(View view) {
        // 注册一个广播，这个广播主要是用于在GalleryActivity进行预览时，防止当所有图片都删除完后，再回到该页面时被取消选中的图片仍处于选中状态
        PublicWay.activityList.add(this);
        position = Integer.parseInt(getIntent().getStringExtra("position"));
        // 为发送按钮设置文字
        galleryViewpager.setOnPageChangeListener(pageChangeListener);

        for (int i = 0; i < Bimp.tempSelectBitmap.size(); i++) {
            initListViews(Bimp.tempSelectBitmap.get(i).getBitmap());
        }
        adapter = new MyPageAdapter(listViews);
        galleryViewpager.setAdapter(adapter);
        galleryViewpager.setPageMargin((int) getResources().getDimensionPixelOffset(R.dimen.dp10));
        int id = getIntent().getIntExtra("ID", 0);
        galleryViewpager.setCurrentItem(id);
        barTvOther.setVisibility(View.VISIBLE);
        barTvOther.setText("删除");
        barTvTitle.setText(location + 1 + "/" + Bimp.tempSelectBitmap.size());
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void clickListener(View view) {

    }

    private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

        public void onPageSelected(int arg0) {
            location = arg0;
            barTvTitle.setText(location + 1 + "/" + Bimp.tempSelectBitmap.size());
        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        public void onPageScrollStateChanged(int arg0) {

        }
    };

    private void initListViews(Bitmap bm) {
        if (listViews == null)
            listViews = new ArrayList<View>();
        PhotoView img = new PhotoView(this);
        img.setBackgroundColor(0xff000000);
        img.setImageBitmap(bm);
        img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        listViews.add(img);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bar_img_back, R.id.bar_tv_other})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_img_back:
                finish();
                break;
            case R.id.bar_tv_other:
                if (listViews.size() == 1) {
                    Bimp.tempSelectBitmap.clear();
                    Bimp.max = 0;
                    barTvTitle.setText(location + 1 + "/" + Bimp.tempSelectBitmap.size());
                    Intent intent = new Intent("data.broadcast.action");
                    sendBroadcast(intent);
                    finish();
                } else {
                    Bimp.tempSelectBitmap.remove(location);
                    Bimp.max--;
                    galleryViewpager.removeAllViews();
                    listViews.remove(location);
                    adapter.setListViews(listViews);
                    barTvTitle.setText(location + 1 + "/" + Bimp.tempSelectBitmap.size());
                    adapter.notifyDataSetChanged();
                }
                break;
        }
    }

    /**
     * viewpage图片展示
     */
    class MyPageAdapter extends PagerAdapter {

        private ArrayList<View> listViews;

        private int size;

        public MyPageAdapter(ArrayList<View> listViews) {
            this.listViews = listViews;
            size = listViews == null ? 0 : listViews.size();
        }

        public void setListViews(ArrayList<View> listViews) {
            this.listViews = listViews;
            size = listViews == null ? 0 : listViews.size();
        }

        public int getCount() {
            return size;
        }

        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPagerFixed) arg0).removeView(listViews.get(arg1 % size));
        }

        public void finishUpdate(View arg0) {
        }

        public Object instantiateItem(View arg0, int arg1) {
            try {
                ((ViewPagerFixed) arg0).addView(listViews.get(arg1 % size), 0);

            } catch (Exception e) {
            }
            return listViews.get(arg1 % size);
        }

        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

    }

}
